package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.*;
import com.luo.service.ClassService;
import com.luo.service.DetailsService;
import com.luo.service.GoodService;
import com.luo.service.MainPicturesService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：
 * 作者：Luo。
 * 日期：2024/1/20 11:00
 */

@WebServlet("/goods")
public class ListGoodsInfoController extends HttpServlet {

    GoodService goodService = new GoodService();
    ClassService classService = new ClassService();
    MainPicturesService mainPicturesService = new MainPicturesService();
    DetailsService detailsService = new DetailsService();
    ResultObj result = new ResultObj();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //根据id查询商品信息
        String id = req.getParameter("id");
        Goods goods = goodService.queryGoodBy(id);
        //根据商品信息查出属于的分类
        OneClass oneClass = classService.queryOneClassByOneId(goods.getOneClassId());
        TwoClass twoClass = classService.queryTwoClassByTwoId(goods.getTwoClassId());
        ArrayList<Object> list = new ArrayList<>();
        list.add(twoClass);
        list.add(oneClass);
        goods.setCategories(list);
        //根据商品信息查出商品详情页首页的图片
        List<GoodPictures> goodPictures = mainPicturesService.queryMainPictureByGoodId(String.valueOf(goods.getId()));
        ArrayList mainPicturesLinks = new ArrayList<>();
        for (GoodPictures mainPic : goodPictures) {
            mainPicturesLinks.add(mainPic.getPicturesLink());
        }
        goods.setMainPictures(mainPicturesLinks);
        //根据商品信息查询商品的文字说明
        List<Properties> properties = detailsService.queryPropByGoodId(id);
        Map<String, Object> map = new HashMap<>();
        map.put("properties", properties);
        //根据商品信息查询商品的图片说明
        List<GoodPictures> pictures = detailsService.queryIntroducePicture(id);
        ArrayList introducePicturesLinks = new ArrayList<>();
        for (GoodPictures pic : pictures) {
            introducePicturesLinks.add(pic.getPicturesLink());
        }
        map.put("pictures", introducePicturesLinks);
        goods.setDetails(map);
        //根据折扣和商品价格算出商品的现价
        Double disCount = Double.valueOf(goods.getDiscount()) / 10;
        Double price = Double.valueOf(goods.getPrice());
        if (disCount > 0.0) {
            goods.setOldPrice(String.format("%.2f", price * disCount));
        } else {
            goods.setOldPrice(String.format("%.2f", price));
        }

        result.setCode("1");
        result.setMsg("查询成功");
        result.setResult(goods);
        String jsonString = JSON.toJSONString(result);
        resp.getWriter().write(jsonString);
        resp.getWriter().close();
    }
}
